<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>threads - Perl interpreter-based threads - Perldoc Browser</title>
    <link rel="search" href="https://perldoc.perl.org/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser">
    <link rel="canonical" href="https://perldoc.perl.org/threads">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" />
    <style>
      body {
        background: #f4f4f5;
        color: #020202;
      }
      .navbar-dark {
        background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%);
        background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%);
        filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0);
        background-repeat: repeat-x;
      }
      .navbar-dark .navbar-nav .nav-link,
      .navbar-dark .navbar-nav .nav-link:focus { color: #fff }
      .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 }
      #wrapperlicious {
        margin: 0 auto;
        font: 0.9em 'Helvetica Neue', Helvetica, sans-serif;
        font-weight: normal;
        line-height: 1.5em;
        margin: 0;
        padding: 0;
      }
      #wrapperlicious h1 { font-size: 1.5em }
      #wrapperlicious h2 { font-size: 1.3em }
      #wrapperlicious h3 { font-size: 1.1em }
      #wrapperlicious h4 { font-size: 0.9em }
      #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3,
      #wrapperlicious h4, #wrapperlicious dt {
        color: #020202;
        margin-top: 1em;
        margin-bottom: 1em;
        position: relative;
        font-weight: bold;
      }
      #wrapperlicious a { color: inherit; text-decoration: underline }
      #wrapperlicious #toc { text-decoration: none }
      #wrapperlicious a:hover { color: #2a2a2a }
      #wrapperlicious a img { border: 0 }
      #wrapperlicious :not(pre) > code {
        color: inherit;
        background-color: rgba(0, 0, 0, 0.04);
        border-radius: 3px;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        padding: 0.3em;
      }
      #wrapperlicious dd {
        margin: 0;
        margin-left: 2em;
      }
      #wrapperlicious dt {
        color: #2a2a2a;
        font-weight: bold;
        margin-left: 0.9em;
      }
      #wrapperlicious p {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious li > p {
        margin-bottom: 0;
        margin-top: 0;
      }
      #wrapperlicious pre {
        border: 1px solid #c1c1c1;
        border-radius: 3px;
        font: 100% Consolas, Menlo, Monaco, monospace;
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious pre > code {
        display: block;
        background-color: #f6f6f6;
        font: 0.9em Consolas, Menlo, Monaco, monospace;
        line-height: 1.5em;
        text-align: left;
        white-space: pre;
        padding: 1em;
      }
      #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul {
        margin-bottom: 1em;
        margin-top: 1em;
      }
      #wrapperlicious ul {
        list-style-type: square;
      }
      #wrapperlicious ul ul {
        margin-bottom: 0px;
        margin-top: 0px;
      }
      #footer {
        font-size: 0.8em;
        padding-top: 0.5em;
        text-align: center;
      }
      #more {
        display: inline;
        font-size: 0.8em;
      }
      #perldocdiv {
        background-color: #fff;
        border: 1px solid #c1c1c1;
        border-bottom-left-radius: 5px;
        border-bottom-right-radius: 5px;
        margin-left: auto;
        margin-right: auto;
        padding: 3em;
        padding-top: 1em;
        max-width: 960px;
      }
      #moduleversion { float: right }
      #wrapperlicious .leading-notice {
        font-style: italic;
        padding-left: 1em;
        margin-top: 1em;
        margin-bottom: 1em;
      }
      #wrapperlicious .permalink {
        display: none;
        left: -0.75em;
        position: absolute;
        padding-right: 0.25em;
        text-decoration: none;
      }
      #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink,
      #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink,
      #wrapperlicious dt:hover .permalink {
        display: block;
      }
    </style>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());

      gtag('config', 'G-KVNWBNT5FB');
      gtag('config', 'UA-50555-3');
    </script>
  </head>
  <body>
    <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="https://perldoc.perl.org/">Perldoc Browser</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="threads.html#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.35.0</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-stable">
          <a class="dropdown-item" href="https://perldoc.perl.org/threads">Latest</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.2/threads">5.38.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.1/threads">5.38.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0/threads">5.38.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.3/threads">5.36.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.2/threads">5.36.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.1/threads">5.36.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.36.0/threads">5.36.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.3/threads">5.34.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.2/threads">5.34.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.1/threads">5.34.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.34.0/threads">5.34.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.1/threads">5.32.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.32.0/threads">5.32.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.3/threads">5.30.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.2/threads">5.30.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.1/threads">5.30.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.30.0/threads">5.30.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.3/threads">5.28.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.2/threads">5.28.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.1/threads">5.28.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.28.0/threads">5.28.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.3/threads">5.26.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.2/threads">5.26.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.1/threads">5.26.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.26.0/threads">5.26.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.4/threads">5.24.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.3/threads">5.24.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.2/threads">5.24.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.1/threads">5.24.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.24.0/threads">5.24.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.4/threads">5.22.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.3/threads">5.22.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.2/threads">5.22.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.1/threads">5.22.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.22.0/threads">5.22.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.3/threads">5.20.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.2/threads">5.20.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.1/threads">5.20.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.20.0/threads">5.20.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.4/threads">5.18.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.3/threads">5.18.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.2/threads">5.18.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.1/threads">5.18.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.18.0/threads">5.18.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.3/threads">5.16.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.2/threads">5.16.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.1/threads">5.16.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.16.0/threads">5.16.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.4/threads">5.14.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.3/threads">5.14.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.2/threads">5.14.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.1/threads">5.14.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.14.0/threads">5.14.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.5/threads">5.12.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.4/threads">5.12.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.3/threads">5.12.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.2/threads">5.12.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.1/threads">5.12.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.12.0/threads">5.12.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.1/threads">5.10.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.10.0/threads">5.10.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.9/threads">5.8.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.8/threads">5.8.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.7/threads">5.8.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.6/threads">5.8.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.5/threads">5.8.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.4/threads">5.8.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.3/threads">5.8.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.2/threads">5.8.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.1/threads">5.8.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.8.0/threads">5.8.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.2/threads">5.6.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.1/threads">5.6.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.6.0/threads">5.6.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_04/threads">5.005_04</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_03/threads">5.005_03</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_02/threads">5.005_02</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005_01/threads">5.005_01</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.005/threads">5.005</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="threads.html#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-dev">
          <a class="dropdown-item" href="https://perldoc.perl.org/blead/threads">blead</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.6/threads">5.39.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.5/threads">5.39.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.4/threads">5.39.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.3/threads">5.39.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.2/threads">5.39.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.39.1/threads">5.39.1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC2/threads">5.38.0-RC2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.38.0-RC1/threads">5.38.0-RC1</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.11/threads">5.37.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.10/threads">5.37.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.9/threads">5.37.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.8/threads">5.37.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.7/threads">5.37.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.6/threads">5.37.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.5/threads">5.37.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.4/threads">5.37.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.3/threads">5.37.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.2/threads">5.37.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.1/threads">5.37.1</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.37.0/threads">5.37.0</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.11/threads">5.35.11</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.10/threads">5.35.10</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.9/threads">5.35.9</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.8/threads">5.35.8</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.7/threads">5.35.7</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.6/threads">5.35.6</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.5/threads">5.35.5</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.4/threads">5.35.4</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.3/threads">5.35.3</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.2/threads">5.35.2</a>
          <a class="dropdown-item" href="https://perldoc.perl.org/5.35.1/threads">5.35.1</a>
          <a class="dropdown-item active" href="threads.html">5.35.0</a>
        </div>
      </li>
      <li class="nav-item dropdown text-nowrap">
        <a class="nav-link dropdown-toggle" href="threads.html#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a>
        <div class="dropdown-menu" aria-labelledby="dropdownlink-nav">
          <a class="dropdown-item" href="perl.html">Perl</a>
          <a class="dropdown-item" href="perlintro.html">Intro</a>
          <a class="dropdown-item" href="perl.html#Tutorials">Tutorials</a>
          <a class="dropdown-item" href="perlfaq.html">FAQs</a>
          <a class="dropdown-item" href="perl.html#Reference-Manual">Reference</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlop.html">Operators</a>
          <a class="dropdown-item" href="functions.html">Functions</a>
          <a class="dropdown-item" href="variables.html">Variables</a>
          <a class="dropdown-item" href="modules.html">Modules</a>
          <a class="dropdown-item" href="perlutil.html">Utilities</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="perlcommunity.html">Community</a>
          <a class="dropdown-item" href="perlhist.html">History</a>
        </div>
      </li>
    </ul>
    <ul class="navbar-nav">
      <script>
        function set_expand (expand) {
          var perldocdiv = document.getElementById('perldocdiv');
          var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width');
          var expanded = (width == '' || width == 'none') ? true : false;
          if (expand === null) {
            expand = !expanded;
          }
          if ((expand && !expanded) || (!expand && expanded)) {
            perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px');
            var button_classlist = document.getElementById('content-expand-button').classList;
            if (expand) {
              button_classlist.add('btn-light');
              button_classlist.remove('btn-outline-light');
            } else {
              button_classlist.add('btn-outline-light');
              button_classlist.remove('btn-light');
            }
          }
          return expand;
        }
        function toggle_expand () {
          var expand = set_expand(null);
          document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC';
        }
        function read_expand () {
          return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 });
        }
        if (document.readyState === 'loading') {
          document.addEventListener('DOMContentLoaded', function () {
            if (read_expand()) {
              set_expand(true);
            }
          });
        } else if (read_expand()) {
          set_expand(true);
        }
      </script>
      <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-2" onclick="toggle_expand()">Expand</button>
    <ul>
    <form class="form-inline" method="get" action="https://perldoc.perl.org/5.35.0/search">
      <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search">
    </form>
  </div>
</nav>

    <div id="wrapperlicious" class="container-fluid">
      <div id="perldocdiv">
        <div id="links">
          <a href="threads.html">threads</a>
          <div id="more">
            (<a href="threads.txt">source</a>,
            <a href="https://metacpan.org/pod/threads">CPAN</a>)
          </div>
            <div id="moduleversion">version 2.26</div>
        </div>
        <div class="leading-notice">
          You are viewing the version of this documentation from Perl 5.35.0.
            This is a development version of Perl.
        </div>
        <h1><a id="toc">CONTENTS</a></h1>
                  <ul>
              <li>
                <a class="text-decoration-none" href="threads.html#NAME">NAME</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#VERSION">VERSION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#WARNING">WARNING</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#SYNOPSIS">SYNOPSIS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#DESCRIPTION">DESCRIPTION</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#EXITING-A-THREAD">EXITING A THREAD</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#THREAD-STATE">THREAD STATE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#THREAD-CONTEXT">THREAD CONTEXT</a>
                            <ul>
              <li>
                <a class="text-decoration-none" href="threads.html#Explicit-context">Explicit context</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#Implicit-context">Implicit context</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#$thr-%3Ewantarray()">$thr-&gt;wantarray()</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#threads-%3Ewantarray()">threads-&gt;wantarray()</a>
              </li>
          </ul>

              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#THREAD-STACK-SIZE">THREAD STACK SIZE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#THREAD-SIGNALLING">THREAD SIGNALLING</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#WARNINGS">WARNINGS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#ERRORS">ERRORS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#BUGS-AND-LIMITATIONS">BUGS AND LIMITATIONS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#REQUIREMENTS">REQUIREMENTS</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#SEE-ALSO">SEE ALSO</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#AUTHOR">AUTHOR</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#LICENSE">LICENSE</a>
              </li>
              <li>
                <a class="text-decoration-none" href="threads.html#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a>
              </li>
          </ul>

      <h1 id="NAME"><a class="permalink" href="threads.html#NAME">#</a>NAME</h1>

<p>threads - Perl interpreter-based threads</p>

<h1 id="VERSION"><a class="permalink" href="threads.html#VERSION">#</a>VERSION</h1>

<p>This document describes threads version 2.26</p>

<h1 id="WARNING"><a class="permalink" href="threads.html#WARNING">#</a>WARNING</h1>

<p>The &quot;interpreter-based threads&quot; provided by Perl are not the fast, lightweight system for multitasking that one might expect or hope for. Threads are implemented in a way that makes them easy to misuse. Few people know how to use them correctly or will be able to provide help.</p>

<p>The use of interpreter-based threads in perl is officially <a href="perlpolicy.html#discouraged">discouraged</a>.</p>

<h1 id="SYNOPSIS"><a class="permalink" href="threads.html#SYNOPSIS">#</a>SYNOPSIS</h1>

<pre><code>use threads (&#39;yield&#39;,
             &#39;stack_size&#39; =&gt; 64*4096,
             &#39;exit&#39; =&gt; &#39;threads_only&#39;,
             &#39;stringify&#39;);

sub start_thread {
    my @args = @_;
    print(&#39;Thread started: &#39;, join(&#39; &#39;, @args), &quot;\n&quot;);
}
my $thr = threads-&gt;create(&#39;start_thread&#39;, &#39;argument&#39;);
$thr-&gt;join();

threads-&gt;create(sub { print(&quot;I am a thread\n&quot;); })-&gt;join();

my $thr2 = async { foreach (@files) { ... } };
$thr2-&gt;join();
if (my $err = $thr2-&gt;error()) {
    warn(&quot;Thread error: $err\n&quot;);
}

# Invoke thread in list context (implicit) so it can return a list
my ($thr) = threads-&gt;create(sub { return (qw/a b c/); });
# or specify list context explicitly
my $thr = threads-&gt;create({&#39;context&#39; =&gt; &#39;list&#39;},
                          sub { return (qw/a b c/); });
my @results = $thr-&gt;join();

$thr-&gt;detach();

# Get a thread&#39;s object
$thr = threads-&gt;self();
$thr = threads-&gt;object($tid);

# Get a thread&#39;s ID
$tid = threads-&gt;tid();
$tid = $thr-&gt;tid();
$tid = &quot;$thr&quot;;

# Give other threads a chance to run
threads-&gt;yield();
yield();

# Lists of non-detached threads
my @threads = threads-&gt;list();
my $thread_count = threads-&gt;list();

my @running = threads-&gt;list(threads::running);
my @joinable = threads-&gt;list(threads::joinable);

# Test thread objects
if ($thr1 == $thr2) {
    ...
}

# Manage thread stack size
$stack_size = threads-&gt;get_stack_size();
$old_size = threads-&gt;set_stack_size(32*4096);

# Create a thread with a specific context and stack size
my $thr = threads-&gt;create({ &#39;context&#39;    =&gt; &#39;list&#39;,
                            &#39;stack_size&#39; =&gt; 32*4096,
                            &#39;exit&#39;       =&gt; &#39;thread_only&#39; },
                          \&amp;foo);

# Get thread&#39;s context
my $wantarray = $thr-&gt;wantarray();

# Check thread&#39;s state
if ($thr-&gt;is_running()) {
    sleep(1);
}
if ($thr-&gt;is_joinable()) {
    $thr-&gt;join();
}

# Send a signal to a thread
$thr-&gt;kill(&#39;SIGUSR1&#39;);

# Exit a thread
threads-&gt;exit();</code></pre>

<h1 id="DESCRIPTION"><a class="permalink" href="threads.html#DESCRIPTION">#</a>DESCRIPTION</h1>

<p>Since Perl 5.8, thread programming has been available using a model called <i>interpreter threads</i> which provides a new Perl interpreter for each thread, and, by default, results in no data or state information being shared between threads.</p>

<p>(Prior to Perl 5.8, <i>5005threads</i> was available through the <code>Thread.pm</code> API. This threading model has been deprecated, and was removed as of Perl 5.10.0.)</p>

<p>As just mentioned, all variables are, by default, thread local. To use shared variables, you need to also load <a href="./threads::shared.html">threads::shared</a>:</p>

<pre><code>use threads;
use threads::shared;</code></pre>

<p>When loading <a href="./threads::shared.html">threads::shared</a>, you must <code>use threads</code> before you <code>use threads::shared</code>. (<code>threads</code> will emit a warning if you do it the other way around.)</p>

<p>It is strongly recommended that you enable threads via <code>use threads</code> as early as possible in your script.</p>

<p>If needed, scripts can be written so as to run on both threaded and non-threaded Perls:</p>

<pre><code>my $can_use_threads = eval &#39;use threads; 1&#39;;
if ($can_use_threads) {
    # Do processing using threads
    ...
} else {
    # Do it without using threads
    ...
}</code></pre>

<dl>

<dt id="$thr-=-threads-&gt;create(FUNCTION,-ARGS)"><a class="permalink" href="threads.html#$thr-=-threads-%3Ecreate(FUNCTION,-ARGS)">#</a><a id="thr-threads-create-FUNCTION-ARGS"></a>$thr = threads-&gt;create(FUNCTION, ARGS)</dt>
<dd>

<p>This will create a new thread that will begin execution with the specified entry point function, and give it the <i>ARGS</i> list as parameters. It will return the corresponding threads object, or <code>undef</code> if thread creation failed.</p>

<p><i>FUNCTION</i> may either be the name of a function, an anonymous subroutine, or a code ref.</p>

<pre><code>my $thr = threads-&gt;create(&#39;func_name&#39;, ...);
    # or
my $thr = threads-&gt;create(sub { ... }, ...);
    # or
my $thr = threads-&gt;create(\&amp;func, ...);</code></pre>

<p>The <code>-&gt;new()</code> method is an alias for <code>-&gt;create()</code>.</p>

</dd>
<dt id="$thr-&gt;join()"><a class="permalink" href="threads.html#$thr-%3Ejoin()">#</a><a id="join"></a><a id="thr-join"></a>$thr-&gt;join()</dt>
<dd>

<p>This will wait for the corresponding thread to complete its execution. When the thread finishes, <code>-&gt;join()</code> will return the return value(s) of the entry point function.</p>

<p>The context (void, scalar or list) for the return value(s) for <code>-&gt;join()</code> is determined at the time of thread creation.</p>

<pre><code># Create thread in list context (implicit)
my ($thr1) = threads-&gt;create(sub {
                                my @results = qw(a b c);
                                return (@results);
                             });
#   or (explicit)
my $thr1 = threads-&gt;create({&#39;context&#39; =&gt; &#39;list&#39;},
                           sub {
                                my @results = qw(a b c);
                                return (@results);
                           });
# Retrieve list results from thread
my @res1 = $thr1-&gt;join();

# Create thread in scalar context (implicit)
my $thr2 = threads-&gt;create(sub {
                                my $result = 42;
                                return ($result);
                             });
# Retrieve scalar result from thread
my $res2 = $thr2-&gt;join();

# Create a thread in void context (explicit)
my $thr3 = threads-&gt;create({&#39;void&#39; =&gt; 1},
                           sub { print(&quot;Hello, world\n&quot;); });
# Join the thread in void context (i.e., no return value)
$thr3-&gt;join();</code></pre>

<p>See <a href="threads.html#THREAD-CONTEXT">&quot;THREAD CONTEXT&quot;</a> for more details.</p>

<p>If the program exits without all threads having either been joined or detached, then a warning will be issued.</p>

<p>Calling <code>-&gt;join()</code> or <code>-&gt;detach()</code> on an already joined thread will cause an error to be thrown.</p>

</dd>
<dt id="$thr-&gt;detach()"><a class="permalink" href="threads.html#$thr-%3Edetach()">#</a><a id="detach"></a><a id="thr-detach"></a>$thr-&gt;detach()</dt>
<dd>

<p>Makes the thread unjoinable, and causes any eventual return value to be discarded. When the program exits, any detached threads that are still running are silently terminated.</p>

<p>If the program exits without all threads having either been joined or detached, then a warning will be issued.</p>

<p>Calling <code>-&gt;join()</code> or <code>-&gt;detach()</code> on an already detached thread will cause an error to be thrown.</p>

</dd>
<dt id="threads-&gt;detach()"><a class="permalink" href="threads.html#threads-%3Edetach()">#</a><a id="threads"></a><a id="threads-detach"></a>threads-&gt;detach()</dt>
<dd>

<p>Class method that allows a thread to detach itself.</p>

</dd>
<dt id="threads-&gt;self()"><a class="permalink" href="threads.html#threads-%3Eself()">#</a><a id="threads1"></a><a id="threads-self"></a>threads-&gt;self()</dt>
<dd>

<p>Class method that allows a thread to obtain its own <i>threads</i> object.</p>

</dd>
<dt id="$thr-&gt;tid()"><a class="permalink" href="threads.html#$thr-%3Etid()">#</a><a id="tid"></a><a id="thr-tid"></a>$thr-&gt;tid()</dt>
<dd>

<p>Returns the ID of the thread. Thread IDs are unique integers with the main thread in a program being 0, and incrementing by 1 for every thread created.</p>

</dd>
<dt id="threads-&gt;tid()"><a class="permalink" href="threads.html#threads-%3Etid()">#</a><a id="threads2"></a><a id="threads-tid"></a>threads-&gt;tid()</dt>
<dd>

<p>Class method that allows a thread to obtain its own ID.</p>

</dd>
<dt id="&quot;$thr&quot;"><a class="permalink" href="threads.html#%22$thr%22">#</a><a id="thr"></a>&quot;$thr&quot;</dt>
<dd>

<p>If you add the <code>stringify</code> import option to your <code>use threads</code> declaration, then using a threads object in a string or a string context (e.g., as a hash key) will cause its ID to be used as the value:</p>

<pre><code>use threads qw(stringify);

my $thr = threads-&gt;create(...);
print(&quot;Thread $thr started\n&quot;);  # Prints: Thread 1 started</code></pre>

</dd>
<dt id="threads-&gt;object($tid)"><a class="permalink" href="threads.html#threads-%3Eobject($tid)">#</a><a id="threads3"></a><a id="threads-object-tid"></a>threads-&gt;object($tid)</dt>
<dd>

<p>This will return the <i>threads</i> object for the <i>active</i> thread associated with the specified thread ID. If <code>$tid</code> is the value for the current thread, then this call works the same as <code>-&gt;self()</code>. Otherwise, returns <code>undef</code> if there is no thread associated with the TID, if the thread is joined or detached, if no TID is specified or if the specified TID is undef.</p>

</dd>
<dt id="threads-&gt;yield()"><a class="permalink" href="threads.html#threads-%3Eyield()">#</a><a id="threads4"></a><a id="threads-yield"></a>threads-&gt;yield()</dt>
<dd>

<p>This is a suggestion to the OS to let this thread yield CPU time to other threads. What actually happens is highly dependent upon the underlying thread implementation.</p>

<p>You may do <code>use threads qw(yield)</code>, and then just use <code>yield()</code> in your code.</p>

</dd>
<dt id="threads-&gt;list()"><a class="permalink" href="threads.html#threads-%3Elist()">#</a><a id="threads5"></a><a id="threads-list"></a>threads-&gt;list()</dt>
<dd>

</dd>
<dt id="threads-&gt;list(threads::all)"><a class="permalink" href="threads.html#threads-%3Elist(threads::all)">#</a><a id="threads6"></a><a id="threads-list-threads::all"></a>threads-&gt;list(threads::all)</dt>
<dd>

</dd>
<dt id="threads-&gt;list(threads::running)"><a class="permalink" href="threads.html#threads-%3Elist(threads::running)">#</a><a id="threads7"></a><a id="threads-list-threads::running"></a>threads-&gt;list(threads::running)</dt>
<dd>

</dd>
<dt id="threads-&gt;list(threads::joinable)"><a class="permalink" href="threads.html#threads-%3Elist(threads::joinable)">#</a><a id="threads8"></a><a id="threads-list-threads::joinable"></a>threads-&gt;list(threads::joinable)</dt>
<dd>

<p>With no arguments (or using <code>threads::all</code>) and in a list context, returns a list of all non-joined, non-detached <i>threads</i> objects. In a scalar context, returns a count of the same.</p>

<p>With a <i>true</i> argument (using <code>threads::running</code>), returns a list of all non-joined, non-detached <i>threads</i> objects that are still running.</p>

<p>With a <i>false</i> argument (using <code>threads::joinable</code>), returns a list of all non-joined, non-detached <i>threads</i> objects that have finished running (i.e., for which <code>-&gt;join()</code> will not <i>block</i>).</p>

</dd>
<dt id="$thr1-&gt;equal($thr2)"><a class="permalink" href="threads.html#$thr1-%3Eequal($thr2)">#</a><a id="equal"></a><a id="thr1-equal-thr2"></a>$thr1-&gt;equal($thr2)</dt>
<dd>

<p>Tests if two threads objects are the same thread or not. This is overloaded to the more natural forms:</p>

<pre><code>if ($thr1 == $thr2) {
    print(&quot;Threads are the same\n&quot;);
}
# or
if ($thr1 != $thr2) {
    print(&quot;Threads differ\n&quot;);
}</code></pre>

<p>(Thread comparison is based on thread IDs.)</p>

</dd>
<dt id="async-BLOCK;"><a class="permalink" href="threads.html#async-BLOCK;">#</a><a id="async"></a><a id="async-BLOCK"></a>async BLOCK;</dt>
<dd>

<p><code>async</code> creates a thread to execute the block immediately following it. This block is treated as an anonymous subroutine, and so must have a semicolon after the closing brace. Like <code>threads-&gt;create()</code>, <code>async</code> returns a <i>threads</i> object.</p>

</dd>
<dt id="$thr-&gt;error()"><a class="permalink" href="threads.html#$thr-%3Eerror()">#</a><a id="error"></a><a id="thr-error"></a>$thr-&gt;error()</dt>
<dd>

<p>Threads are executed in an <code>eval</code> context. This method will return <code>undef</code> if the thread terminates <i>normally</i>. Otherwise, it returns the value of <code>$@</code> associated with the thread&#39;s execution status in its <code>eval</code> context.</p>

</dd>
<dt id="$thr-&gt;_handle()"><a class="permalink" href="threads.html#$thr-%3E_handle()">#</a><a id="_handle"></a><a id="thr-_handle"></a>$thr-&gt;_handle()</dt>
<dd>

<p>This <i>private</i> method returns a pointer (i.e., the memory location expressed as an unsigned integer) to the internal thread structure associated with a threads object. For Win32, this is a pointer to the <code>HANDLE</code> value returned by <code>CreateThread</code> (i.e., <code>HANDLE *</code>); for other platforms, it is a pointer to the <code>pthread_t</code> structure used in the <code>pthread_create</code> call (i.e., <code>pthread_t *</code>).</p>

<p>This method is of no use for general Perl threads programming. Its intent is to provide other (XS-based) thread modules with the capability to access, and possibly manipulate, the underlying thread structure associated with a Perl thread.</p>

</dd>
<dt id="threads-&gt;_handle()"><a class="permalink" href="threads.html#threads-%3E_handle()">#</a><a id="threads9"></a><a id="threads-_handle"></a>threads-&gt;_handle()</dt>
<dd>

<p>Class method that allows a thread to obtain its own <i>handle</i>.</p>

</dd>
</dl>

<h1 id="EXITING-A-THREAD"><a class="permalink" href="threads.html#EXITING-A-THREAD">#</a><a id="EXITING"></a>EXITING A THREAD</h1>

<p>The usual method for terminating a thread is to <a href="perlfunc.html#return-EXPR">return()</a> from the entry point function with the appropriate return value(s).</p>

<dl>

<dt id="threads-&gt;exit()"><a class="permalink" href="threads.html#threads-%3Eexit()">#</a><a id="threads10"></a><a id="threads-exit"></a>threads-&gt;exit()</dt>
<dd>

<p>If needed, a thread can be exited at any time by calling <code>threads-&gt;exit()</code>. This will cause the thread to return <code>undef</code> in a scalar context, or the empty list in a list context.</p>

<p>When called from the <i>main</i> thread, this behaves the same as <code>exit(0)</code>.</p>

</dd>
<dt id="threads-&gt;exit(status)"><a class="permalink" href="threads.html#threads-%3Eexit(status)">#</a><a id="threads11"></a><a id="threads-exit-status"></a>threads-&gt;exit(status)</dt>
<dd>

<p>When called from a thread, this behaves like <code>threads-&gt;exit()</code> (i.e., the exit status code is ignored).</p>

<p>When called from the <i>main</i> thread, this behaves the same as <code>exit(status)</code>.</p>

</dd>
<dt id="die()"><a class="permalink" href="threads.html#die()">#</a><a id="die"></a>die()</dt>
<dd>

<p>Calling <code>die()</code> in a thread indicates an abnormal exit for the thread. Any <code>$SIG{__DIE__}</code> handler in the thread will be called first, and then the thread will exit with a warning message that will contain any arguments passed in the <code>die()</code> call.</p>

</dd>
<dt id="exit(status)"><a class="permalink" href="threads.html#exit(status)">#</a><a id="exit"></a><a id="exit-status"></a>exit(status)</dt>
<dd>

<p>Calling <a href="perlfunc.html#exit-EXPR">exit()</a> inside a thread causes the whole application to terminate. Because of this, the use of <code>exit()</code> inside threaded code, or in modules that might be used in threaded applications, is strongly discouraged.</p>

<p>If <code>exit()</code> really is needed, then consider using the following:</p>

<pre><code>threads-&gt;exit() if threads-&gt;can(&#39;exit&#39;);   # Thread friendly
exit(status);</code></pre>

</dd>
<dt id="use-threads-&#39;exit&#39;-=&gt;-&#39;threads_only&#39;"><a class="permalink" href="threads.html#use-threads-&#39;exit&#39;-=%3E-&#39;threads_only&#39;">#</a><a id="use"></a><a id="use-threads-exit-threads_only"></a>use threads &#39;exit&#39; =&gt; &#39;threads_only&#39;</dt>
<dd>

<p>This globally overrides the default behavior of calling <code>exit()</code> inside a thread, and effectively causes such calls to behave the same as <code>threads-&gt;exit()</code>. In other words, with this setting, calling <code>exit()</code> causes only the thread to terminate.</p>

<p>Because of its global effect, this setting should not be used inside modules or the like.</p>

<p>The <i>main</i> thread is unaffected by this setting.</p>

</dd>
<dt id="threads-&gt;create({&#39;exit&#39;-=&gt;-&#39;thread_only&#39;},-...)"><a class="permalink" href="threads.html#threads-%3Ecreate(%7B&#39;exit&#39;-=%3E-&#39;thread_only&#39;%7D,-...)">#</a><a id="threads12"></a><a id="threads-create-exit-thread_only"></a>threads-&gt;create({&#39;exit&#39; =&gt; &#39;thread_only&#39;}, ...)</dt>
<dd>

<p>This overrides the default behavior of <code>exit()</code> inside the newly created thread only.</p>

</dd>
<dt id="$thr-&gt;set_thread_exit_only(boolean)"><a class="permalink" href="threads.html#$thr-%3Eset_thread_exit_only(boolean)">#</a><a id="set_thread_exit_only"></a><a id="thr-set_thread_exit_only-boolean"></a>$thr-&gt;set_thread_exit_only(boolean)</dt>
<dd>

<p>This can be used to change the <i>exit thread only</i> behavior for a thread after it has been created. With a <i>true</i> argument, <code>exit()</code> will cause only the thread to exit. With a <i>false</i> argument, <code>exit()</code> will terminate the application.</p>

<p>The <i>main</i> thread is unaffected by this call.</p>

</dd>
<dt id="threads-&gt;set_thread_exit_only(boolean)"><a class="permalink" href="threads.html#threads-%3Eset_thread_exit_only(boolean)">#</a><a id="threads13"></a><a id="threads-set_thread_exit_only-boolean"></a>threads-&gt;set_thread_exit_only(boolean)</dt>
<dd>

<p>Class method for use inside a thread to change its own behavior for <code>exit()</code>.</p>

<p>The <i>main</i> thread is unaffected by this call.</p>

</dd>
</dl>

<h1 id="THREAD-STATE"><a class="permalink" href="threads.html#THREAD-STATE">#</a><a id="THREAD"></a>THREAD STATE</h1>

<p>The following boolean methods are useful in determining the <i>state</i> of a thread.</p>

<dl>

<dt id="$thr-&gt;is_running()"><a class="permalink" href="threads.html#$thr-%3Eis_running()">#</a><a id="is_running"></a><a id="thr-is_running"></a>$thr-&gt;is_running()</dt>
<dd>

<p>Returns true if a thread is still running (i.e., if its entry point function has not yet finished or exited).</p>

</dd>
<dt id="$thr-&gt;is_joinable()"><a class="permalink" href="threads.html#$thr-%3Eis_joinable()">#</a><a id="is_joinable"></a><a id="thr-is_joinable"></a>$thr-&gt;is_joinable()</dt>
<dd>

<p>Returns true if the thread has finished running, is not detached and has not yet been joined. In other words, the thread is ready to be joined, and a call to <code>$thr-&gt;join()</code> will not <i>block</i>.</p>

</dd>
<dt id="$thr-&gt;is_detached()"><a class="permalink" href="threads.html#$thr-%3Eis_detached()">#</a><a id="is_detached"></a><a id="thr-is_detached"></a>$thr-&gt;is_detached()</dt>
<dd>

<p>Returns true if the thread has been detached.</p>

</dd>
<dt id="threads-&gt;is_detached()"><a class="permalink" href="threads.html#threads-%3Eis_detached()">#</a><a id="threads14"></a><a id="threads-is_detached"></a>threads-&gt;is_detached()</dt>
<dd>

<p>Class method that allows a thread to determine whether or not it is detached.</p>

</dd>
</dl>

<h1 id="THREAD-CONTEXT"><a class="permalink" href="threads.html#THREAD-CONTEXT">#</a><a id="THREAD1"></a>THREAD CONTEXT</h1>

<p>As with subroutines, the type of value returned from a thread&#39;s entry point function may be determined by the thread&#39;s <i>context</i>: list, scalar or void. The thread&#39;s context is determined at thread creation. This is necessary so that the context is available to the entry point function via <a href="perlfunc.html#wantarray">wantarray()</a>. The thread may then specify a value of the appropriate type to be returned from <code>-&gt;join()</code>.</p>

<h2 id="Explicit-context"><a class="permalink" href="threads.html#Explicit-context">#</a><a id="Explicit"></a>Explicit context</h2>

<p>Because thread creation and thread joining may occur in different contexts, it may be desirable to state the context explicitly to the thread&#39;s entry point function. This may be done by calling <code>-&gt;create()</code> with a hash reference as the first argument:</p>

<pre><code>my $thr = threads-&gt;create({&#39;context&#39; =&gt; &#39;list&#39;}, \&amp;foo);
...
my @results = $thr-&gt;join();</code></pre>

<p>In the above, the threads object is returned to the parent thread in scalar context, and the thread&#39;s entry point function <code>foo</code> will be called in list (array) context such that the parent thread can receive a list (array) from the <code>-&gt;join()</code> call. (<code>&#39;array&#39;</code> is synonymous with <code>&#39;list&#39;</code>.)</p>

<p>Similarly, if you need the threads object, but your thread will not be returning a value (i.e., <i>void</i> context), you would do the following:</p>

<pre><code>my $thr = threads-&gt;create({&#39;context&#39; =&gt; &#39;void&#39;}, \&amp;foo);
...
$thr-&gt;join();</code></pre>

<p>The context type may also be used as the <i>key</i> in the hash reference followed by a <i>true</i> value:</p>

<pre><code>threads-&gt;create({&#39;scalar&#39; =&gt; 1}, \&amp;foo);
...
my ($thr) = threads-&gt;list();
my $result = $thr-&gt;join();</code></pre>

<h2 id="Implicit-context"><a class="permalink" href="threads.html#Implicit-context">#</a><a id="Implicit"></a>Implicit context</h2>

<p>If not explicitly stated, the thread&#39;s context is implied from the context of the <code>-&gt;create()</code> call:</p>

<pre><code># Create thread in list context
my ($thr) = threads-&gt;create(...);

# Create thread in scalar context
my $thr = threads-&gt;create(...);

# Create thread in void context
threads-&gt;create(...);</code></pre>

<h2 id="$thr-&gt;wantarray()"><a class="permalink" href="threads.html#$thr-%3Ewantarray()">#</a><a id="wantarray"></a><a id="thr-wantarray"></a>$thr-&gt;wantarray()</h2>

<p>This returns the thread&#39;s context in the same manner as <a href="perlfunc.html#wantarray">wantarray()</a>.</p>

<h2 id="threads-&gt;wantarray()"><a class="permalink" href="threads.html#threads-%3Ewantarray()">#</a><a id="threads15"></a><a id="threads-wantarray"></a>threads-&gt;wantarray()</h2>

<p>Class method to return the current thread&#39;s context. This returns the same value as running <a href="perlfunc.html#wantarray">wantarray()</a> inside the current thread&#39;s entry point function.</p>

<h1 id="THREAD-STACK-SIZE"><a class="permalink" href="threads.html#THREAD-STACK-SIZE">#</a><a id="THREAD2"></a>THREAD STACK SIZE</h1>

<p>The default per-thread stack size for different platforms varies significantly, and is almost always far more than is needed for most applications. On Win32, Perl&#39;s makefile explicitly sets the default stack to 16 MB; on most other platforms, the system default is used, which again may be much larger than is needed.</p>

<p>By tuning the stack size to more accurately reflect your application&#39;s needs, you may significantly reduce your application&#39;s memory usage, and increase the number of simultaneously running threads.</p>

<p>Note that on Windows, address space allocation granularity is 64 KB, therefore, setting the stack smaller than that on Win32 Perl will not save any more memory.</p>

<dl>

<dt id="threads-&gt;get_stack_size();"><a class="permalink" href="threads.html#threads-%3Eget_stack_size();">#</a><a id="threads16"></a><a id="threads-get_stack_size"></a>threads-&gt;get_stack_size();</dt>
<dd>

<p>Returns the current default per-thread stack size. The default is zero, which means the system default stack size is currently in use.</p>

</dd>
<dt id="$size-=-$thr-&gt;get_stack_size();"><a class="permalink" href="threads.html#$size-=-$thr-%3Eget_stack_size();">#</a><a id="size-thr-get_stack_size"></a>$size = $thr-&gt;get_stack_size();</dt>
<dd>

<p>Returns the stack size for a particular thread. A return value of zero indicates the system default stack size was used for the thread.</p>

</dd>
<dt id="$old_size-=-threads-&gt;set_stack_size($new_size);"><a class="permalink" href="threads.html#$old_size-=-threads-%3Eset_stack_size($new_size);">#</a><a id="old_size-threads-set_stack_size-new_size"></a>$old_size = threads-&gt;set_stack_size($new_size);</dt>
<dd>

<p>Sets a new default per-thread stack size, and returns the previous setting.</p>

<p>Some platforms have a minimum thread stack size. Trying to set the stack size below this value will result in a warning, and the minimum stack size will be used.</p>

<p>Some Linux platforms have a maximum stack size. Setting too large of a stack size will cause thread creation to fail.</p>

<p>If needed, <code>$new_size</code> will be rounded up to the next multiple of the memory page size (usually 4096 or 8192).</p>

<p>Threads created after the stack size is set will then either call <code>pthread_attr_setstacksize()</code> <i>(for pthreads platforms)</i>, or supply the stack size to <code>CreateThread()</code> <i>(for Win32 Perl)</i>.</p>

<p>(Obviously, this call does not affect any currently extant threads.)</p>

</dd>
<dt id="use-threads-(&#39;stack_size&#39;-=&gt;-VALUE);"><a class="permalink" href="threads.html#use-threads-(&#39;stack_size&#39;-=%3E-VALUE);">#</a><a id="use1"></a><a id="use-threads-stack_size-VALUE"></a>use threads (&#39;stack_size&#39; =&gt; VALUE);</dt>
<dd>

<p>This sets the default per-thread stack size at the start of the application.</p>

</dd>
<dt id="$ENV{&#39;PERL5_ITHREADS_STACK_SIZE&#39;}"><a class="permalink" href="threads.html#$ENV%7B&#39;PERL5_ITHREADS_STACK_SIZE&#39;%7D">#</a><a id="ENV-PERL5_ITHREADS_STACK_SIZE"></a>$ENV{&#39;PERL5_ITHREADS_STACK_SIZE&#39;}</dt>
<dd>

<p>The default per-thread stack size may be set at the start of the application through the use of the environment variable <code>PERL5_ITHREADS_STACK_SIZE</code>:</p>

<pre><code>PERL5_ITHREADS_STACK_SIZE=1048576
export PERL5_ITHREADS_STACK_SIZE
perl -e&#39;use threads; print(threads-&gt;get_stack_size(), &quot;\n&quot;)&#39;</code></pre>

<p>This value overrides any <code>stack_size</code> parameter given to <code>use threads</code>. Its primary purpose is to permit setting the per-thread stack size for legacy threaded applications.</p>

</dd>
<dt id="threads-&gt;create({&#39;stack_size&#39;-=&gt;-VALUE},-FUNCTION,-ARGS)"><a class="permalink" href="threads.html#threads-%3Ecreate(%7B&#39;stack_size&#39;-=%3E-VALUE%7D,-FUNCTION,-ARGS)">#</a><a id="threads17"></a><a id="threads-create-stack_size-VALUE-FUNCTION-ARGS"></a>threads-&gt;create({&#39;stack_size&#39; =&gt; VALUE}, FUNCTION, ARGS)</dt>
<dd>

<p>To specify a particular stack size for any individual thread, call <code>-&gt;create()</code> with a hash reference as the first argument:</p>

<pre><code>my $thr = threads-&gt;create({&#39;stack_size&#39; =&gt; 32*4096},
                          \&amp;foo, @args);</code></pre>

</dd>
<dt id="$thr2-=-$thr1-&gt;create(FUNCTION,-ARGS)"><a class="permalink" href="threads.html#$thr2-=-$thr1-%3Ecreate(FUNCTION,-ARGS)">#</a><a id="thr2-thr1-create-FUNCTION-ARGS"></a>$thr2 = $thr1-&gt;create(FUNCTION, ARGS)</dt>
<dd>

<p>This creates a new thread (<code>$thr2</code>) that inherits the stack size from an existing thread (<code>$thr1</code>). This is shorthand for the following:</p>

<pre><code>my $stack_size = $thr1-&gt;get_stack_size();
my $thr2 = threads-&gt;create({&#39;stack_size&#39; =&gt; $stack_size},
                           FUNCTION, ARGS);</code></pre>

</dd>
</dl>

<h1 id="THREAD-SIGNALLING"><a class="permalink" href="threads.html#THREAD-SIGNALLING">#</a><a id="THREAD3"></a>THREAD SIGNALLING</h1>

<p>When safe signals is in effect (the default behavior - see <a href="threads.html#Unsafe-signals">&quot;Unsafe signals&quot;</a> for more details), then signals may be sent and acted upon by individual threads.</p>

<dl>

<dt id="$thr-&gt;kill(&#39;SIG...&#39;);"><a class="permalink" href="threads.html#$thr-%3Ekill(&#39;SIG...&#39;);">#</a><a id="kill"></a><a id="thr-kill-SIG"></a>$thr-&gt;kill(&#39;SIG...&#39;);</dt>
<dd>

<p>Sends the specified signal to the thread. Signal names and (positive) signal numbers are the same as those supported by <a href="perlfunc.html#kill-SIGNAL%2C-LIST">kill()</a>. For example, &#39;SIGTERM&#39;, &#39;TERM&#39; and (depending on the OS) 15 are all valid arguments to <code>-&gt;kill()</code>.</p>

<p>Returns the thread object to allow for method chaining:</p>

<pre><code>$thr-&gt;kill(&#39;SIG...&#39;)-&gt;join();</code></pre>

</dd>
</dl>

<p>Signal handlers need to be set up in the threads for the signals they are expected to act upon. Here&#39;s an example for <i>cancelling</i> a thread:</p>

<pre><code>use threads;

sub thr_func
{
    # Thread &#39;cancellation&#39; signal handler
    $SIG{&#39;KILL&#39;} = sub { threads-&gt;exit(); };

    ...
}

# Create a thread
my $thr = threads-&gt;create(&#39;thr_func&#39;);

...

# Signal the thread to terminate, and then detach
# it so that it will get cleaned up automatically
$thr-&gt;kill(&#39;KILL&#39;)-&gt;detach();</code></pre>

<p>Here&#39;s another simplistic example that illustrates the use of thread signalling in conjunction with a semaphore to provide rudimentary <i>suspend</i> and <i>resume</i> capabilities:</p>

<pre><code>use threads;
use Thread::Semaphore;

sub thr_func
{
    my $sema = shift;

    # Thread &#39;suspend/resume&#39; signal handler
    $SIG{&#39;STOP&#39;} = sub {
        $sema-&gt;down();      # Thread suspended
        $sema-&gt;up();        # Thread resumes
    };

    ...
}

# Create a semaphore and pass it to a thread
my $sema = Thread::Semaphore-&gt;new();
my $thr = threads-&gt;create(&#39;thr_func&#39;, $sema);

# Suspend the thread
$sema-&gt;down();
$thr-&gt;kill(&#39;STOP&#39;);

...

# Allow the thread to continue
$sema-&gt;up();</code></pre>

<p>CAVEAT: The thread signalling capability provided by this module does not actually send signals via the OS. It <i>emulates</i> signals at the Perl-level such that signal handlers are called in the appropriate thread. For example, sending <code>$thr-&gt;kill(&#39;STOP&#39;)</code> does not actually suspend a thread (or the whole process), but does cause a <code>$SIG{&#39;STOP&#39;}</code> handler to be called in that thread (as illustrated above).</p>

<p>As such, signals that would normally not be appropriate to use in the <code>kill()</code> command (e.g., <code>kill(&#39;KILL&#39;, $$)</code>) are okay to use with the <code>-&gt;kill()</code> method (again, as illustrated above).</p>

<p>Correspondingly, sending a signal to a thread does not disrupt the operation the thread is currently working on: The signal will be acted upon after the current operation has completed. For instance, if the thread is <i>stuck</i> on an I/O call, sending it a signal will not cause the I/O call to be interrupted such that the signal is acted up immediately.</p>

<p>Sending a signal to a terminated/finished thread is ignored.</p>

<h1 id="WARNINGS"><a class="permalink" href="threads.html#WARNINGS">#</a>WARNINGS</h1>

<dl>

<dt id="Perl-exited-with-active-threads:"><a class="permalink" href="threads.html#Perl-exited-with-active-threads:">#</a><a id="Perl"></a><a id="Perl-exited-with-active-threads"></a>Perl exited with active threads:</dt>
<dd>

<p>If the program exits without all threads having either been joined or detached, then this warning will be issued.</p>

<p>NOTE: If the <i>main</i> thread exits, then this warning cannot be suppressed using <code>no warnings &#39;threads&#39;;</code> as suggested below.</p>

</dd>
<dt id="Thread-creation-failed:-pthread_create-returned-#"><a class="permalink" href="threads.html#Thread-creation-failed:-pthread_create-returned-%23">#</a><a id="Thread"></a><a id="Thread-creation-failed:-pthread_create-returned"></a>Thread creation failed: pthread_create returned #</dt>
<dd>

<p>See the appropriate <i>man</i> page for <code>pthread_create</code> to determine the actual cause for the failure.</p>

</dd>
<dt id="Thread-#-terminated-abnormally:-..."><a class="permalink" href="threads.html#Thread-%23-terminated-abnormally:-...">#</a><a id="Thread1"></a><a id="Thread-terminated-abnormally"></a>Thread # terminated abnormally: ...</dt>
<dd>

<p>A thread terminated in some manner other than just returning from its entry point function, or by using <code>threads-&gt;exit()</code>. For example, the thread may have terminated because of an error, or by using <code>die</code>.</p>

</dd>
<dt id="Using-minimum-thread-stack-size-of-#"><a class="permalink" href="threads.html#Using-minimum-thread-stack-size-of-%23">#</a><a id="Using"></a><a id="Using-minimum-thread-stack-size-of"></a>Using minimum thread stack size of #</dt>
<dd>

<p>Some platforms have a minimum thread stack size. Trying to set the stack size below this value will result in the above warning, and the stack size will be set to the minimum.</p>

</dd>
<dt id="Thread-creation-failed:-pthread_attr_setstacksize(SIZE)-returned-22"><a class="permalink" href="threads.html#Thread-creation-failed:-pthread_attr_setstacksize(SIZE)-returned-22">#</a><a id="Thread2"></a><a id="Thread-creation-failed:-pthread_attr_setstacksize-SIZE-returned-22"></a>Thread creation failed: pthread_attr_setstacksize(<i>SIZE</i>) returned 22</dt>
<dd>

<p>The specified <i>SIZE</i> exceeds the system&#39;s maximum stack size. Use a smaller value for the stack size.</p>

</dd>
</dl>

<p>If needed, thread warnings can be suppressed by using:</p>

<pre><code>no warnings &#39;threads&#39;;</code></pre>

<p>in the appropriate scope.</p>

<h1 id="ERRORS"><a class="permalink" href="threads.html#ERRORS">#</a>ERRORS</h1>

<dl>

<dt id="This-Perl-not-built-to-support-threads"><a class="permalink" href="threads.html#This-Perl-not-built-to-support-threads">#</a><a id="This"></a>This Perl not built to support threads</dt>
<dd>

<p>The particular copy of Perl that you&#39;re trying to use was not built using the <code>useithreads</code> configuration option.</p>

<p>Having threads support requires all of Perl and all of the XS modules in the Perl installation to be rebuilt; it is not just a question of adding the <a href="threads.html">threads</a> module (i.e., threaded and non-threaded Perls are binary incompatible).</p>

</dd>
<dt id="Cannot-change-stack-size-of-an-existing-thread"><a class="permalink" href="threads.html#Cannot-change-stack-size-of-an-existing-thread">#</a><a id="Cannot"></a>Cannot change stack size of an existing thread</dt>
<dd>

<p>The stack size of currently extant threads cannot be changed, therefore, the following results in the above error:</p>

<pre><code>$thr-&gt;set_stack_size($size);</code></pre>

</dd>
<dt id="Cannot-signal-threads-without-safe-signals"><a class="permalink" href="threads.html#Cannot-signal-threads-without-safe-signals">#</a><a id="Cannot1"></a>Cannot signal threads without safe signals</dt>
<dd>

<p>Safe signals must be in effect to use the <code>-&gt;kill()</code> signalling method. See <a href="threads.html#Unsafe-signals">&quot;Unsafe signals&quot;</a> for more details.</p>

</dd>
<dt id="Unrecognized-signal-name:-..."><a class="permalink" href="threads.html#Unrecognized-signal-name:-...">#</a><a id="Unrecognized"></a><a id="Unrecognized-signal-name"></a>Unrecognized signal name: ...</dt>
<dd>

<p>The particular copy of Perl that you&#39;re trying to use does not support the specified signal being used in a <code>-&gt;kill()</code> call.</p>

</dd>
</dl>

<h1 id="BUGS-AND-LIMITATIONS"><a class="permalink" href="threads.html#BUGS-AND-LIMITATIONS">#</a><a id="BUGS"></a>BUGS AND LIMITATIONS</h1>

<p>Before you consider posting a bug report, please consult, and possibly post a message to the discussion forum to see if what you&#39;ve encountered is a known problem.</p>

<dl>

<dt id="Thread-safe-modules"><a class="permalink" href="threads.html#Thread-safe-modules">#</a><a id="Thread3"></a>Thread-safe modules</dt>
<dd>

<p>See <a href="perlmod.html#Making-your-module-threadsafe">&quot;Making your module threadsafe&quot; in perlmod</a> when creating modules that may be used in threaded applications, especially if those modules use non-Perl data, or XS code.</p>

</dd>
<dt id="Using-non-thread-safe-modules"><a class="permalink" href="threads.html#Using-non-thread-safe-modules">#</a><a id="Using1"></a>Using non-thread-safe modules</dt>
<dd>

<p>Unfortunately, you may encounter Perl modules that are not <i>thread-safe</i>. For example, they may crash the Perl interpreter during execution, or may dump core on termination. Depending on the module and the requirements of your application, it may be possible to work around such difficulties.</p>

<p>If the module will only be used inside a thread, you can try loading the module from inside the thread entry point function using <code>require</code> (and <code>import</code> if needed):</p>

<pre><code>sub thr_func
{
    require Unsafe::Module
    # Unsafe::Module-&gt;import(...);

    ....
}</code></pre>

<p>If the module is needed inside the <i>main</i> thread, try modifying your application so that the module is loaded (again using <code>require</code> and <code>-&gt;import()</code>) after any threads are started, and in such a way that no other threads are started afterwards.</p>

<p>If the above does not work, or is not adequate for your application, then file a bug report on <a href="https://rt.cpan.org/Public/">https://rt.cpan.org/Public/</a> against the problematic module.</p>

</dd>
<dt id="Memory-consumption"><a class="permalink" href="threads.html#Memory-consumption">#</a><a id="Memory"></a>Memory consumption</dt>
<dd>

<p>On most systems, frequent and continual creation and destruction of threads can lead to ever-increasing growth in the memory footprint of the Perl interpreter. While it is simple to just launch threads and then <code>-&gt;join()</code> or <code>-&gt;detach()</code> them, for long-lived applications, it is better to maintain a pool of threads, and to reuse them for the work needed, using <a href="./Thread::Queue.html">queues</a> to notify threads of pending work. The CPAN distribution of this module contains a simple example (<i>examples/pool_reuse.pl</i>) illustrating the creation, use and monitoring of a pool of <i>reusable</i> threads.</p>

</dd>
<dt id="Current-working-directory"><a class="permalink" href="threads.html#Current-working-directory">#</a><a id="Current"></a>Current working directory</dt>
<dd>

<p>On all platforms except MSWin32, the setting for the current working directory is shared among all threads such that changing it in one thread (e.g., using <code>chdir()</code>) will affect all the threads in the application.</p>

<p>On MSWin32, each thread maintains its own the current working directory setting.</p>

</dd>
<dt id="Locales"><a class="permalink" href="threads.html#Locales">#</a>Locales</dt>
<dd>

<p>Prior to Perl 5.28, locales could not be used with threads, due to various race conditions. Starting in that release, on systems that implement thread-safe locale functions, threads can be used, with some caveats. This includes Windows starting with Visual Studio 2005, and systems compatible with POSIX 2008. See <a href="perllocale.html#Multi-threaded-operation">&quot;Multi-threaded operation&quot; in perllocale</a>.</p>

<p>Each thread (except the main thread) is started using the C locale. The main thread is started like all other Perl programs; see <a href="perllocale.html#ENVIRONMENT">&quot;ENVIRONMENT&quot; in perllocale</a>. You can switch locales in any thread as often as you like.</p>

<p>If you want to inherit the parent thread&#39;s locale, you can, in the parent, set a variable like so:</p>

<pre><code>$foo = POSIX::setlocale(LC_ALL, NULL);</code></pre>

<p>and then pass to threads-&gt;create() a sub that closes over <code>$foo</code>. Then, in the child, you say</p>

<pre><code>POSIX::setlocale(LC_ALL, $foo);</code></pre>

<p>Or you can use the facilities in <a href="./threads::shared.html">threads::shared</a> to pass <code>$foo</code>; or if the environment hasn&#39;t changed, in the child, do</p>

<pre><code>POSIX::setlocale(LC_ALL, &quot;&quot;);</code></pre>

</dd>
<dt id="Environment-variables"><a class="permalink" href="threads.html#Environment-variables">#</a><a id="Environment"></a>Environment variables</dt>
<dd>

<p>Currently, on all platforms except MSWin32, all <i>system</i> calls (e.g., using <code>system()</code> or back-ticks) made from threads use the environment variable settings from the <i>main</i> thread. In other words, changes made to <code>%ENV</code> in a thread will not be visible in <i>system</i> calls made by that thread.</p>

<p>To work around this, set environment variables as part of the <i>system</i> call. For example:</p>

<pre><code>my $msg = &#39;hello&#39;;
system(&quot;FOO=$msg; echo \$FOO&quot;);   # Outputs &#39;hello&#39; to STDOUT</code></pre>

<p>On MSWin32, each thread maintains its own set of environment variables.</p>

</dd>
<dt id="Catching-signals"><a class="permalink" href="threads.html#Catching-signals">#</a><a id="Catching"></a>Catching signals</dt>
<dd>

<p>Signals are <i>caught</i> by the main thread (thread ID = 0) of a script. Therefore, setting up signal handlers in threads for purposes other than <a href="threads.html#THREAD-SIGNALLING">&quot;THREAD SIGNALLING&quot;</a> as documented above will not accomplish what is intended.</p>

<p>This is especially true if trying to catch <code>SIGALRM</code> in a thread. To handle alarms in threads, set up a signal handler in the main thread, and then use <a href="threads.html#THREAD-SIGNALLING">&quot;THREAD SIGNALLING&quot;</a> to relay the signal to the thread:</p>

<pre><code># Create thread with a task that may time out
my $thr = threads-&gt;create(sub {
    threads-&gt;yield();
    eval {
        $SIG{ALRM} = sub { die(&quot;Timeout\n&quot;); };
        alarm(10);
        ...  # Do work here
        alarm(0);
    };
    if ($@ =~ /Timeout/) {
        warn(&quot;Task in thread timed out\n&quot;);
    }
};

# Set signal handler to relay SIGALRM to thread
$SIG{ALRM} = sub { $thr-&gt;kill(&#39;ALRM&#39;) };

... # Main thread continues working</code></pre>

</dd>
<dt id="Parent-child-threads"><a class="permalink" href="threads.html#Parent-child-threads">#</a><a id="Parent"></a>Parent-child threads</dt>
<dd>

<p>On some platforms, it might not be possible to destroy <i>parent</i> threads while there are still existing <i>child</i> threads.</p>

</dd>
<dt id="Unsafe-signals"><a class="permalink" href="threads.html#Unsafe-signals">#</a><a id="Unsafe"></a>Unsafe signals</dt>
<dd>

<p>Since Perl 5.8.0, signals have been made safer in Perl by postponing their handling until the interpreter is in a <i>safe</i> state. See <a href="perl58delta.html#Safe-Signals">&quot;Safe Signals&quot; in perl58delta</a> and <a href="perlipc.html#Deferred-Signals-%28Safe-Signals%29">&quot;Deferred Signals (Safe Signals)&quot; in perlipc</a> for more details.</p>

<p>Safe signals is the default behavior, and the old, immediate, unsafe signalling behavior is only in effect in the following situations:</p>

<ul>

<li><p>Perl has been built with <code>PERL_OLD_SIGNALS</code> (see <code>perl -V</code>).</p>

</li>
<li><p>The environment variable <code>PERL_SIGNALS</code> is set to <code>unsafe</code> (see <a href="perlrun.html#PERL_SIGNALS">&quot;PERL_SIGNALS&quot; in perlrun</a>).</p>

</li>
<li><p>The module <a href="./Perl::Unsafe::Signals.html">Perl::Unsafe::Signals</a> is used.</p>

</li>
</ul>

<p>If unsafe signals is in effect, then signal handling is not thread-safe, and the <code>-&gt;kill()</code> signalling method cannot be used.</p>

</dd>
<dt id="Identity-of-objects-returned-from-threads"><a class="permalink" href="threads.html#Identity-of-objects-returned-from-threads">#</a><a id="Identity"></a>Identity of objects returned from threads</dt>
<dd>

<p>When a value is returned from a thread through a <code>join</code> operation, the value and everything that it references is copied across to the joining thread, in much the same way that values are copied upon thread creation. This works fine for most kinds of value, including arrays, hashes, and subroutines. The copying recurses through array elements, reference scalars, variables closed over by subroutines, and other kinds of reference.</p>

<p>However, everything referenced by the returned value is a fresh copy in the joining thread, even if a returned object had in the child thread been a copy of something that previously existed in the parent thread. After joining, the parent will therefore have a duplicate of each such object. This sometimes matters, especially if the object gets mutated; this can especially matter for private data to which a returned subroutine provides access.</p>

</dd>
<dt id="Returning-blessed-objects-from-threads"><a class="permalink" href="threads.html#Returning-blessed-objects-from-threads">#</a><a id="Returning"></a>Returning blessed objects from threads</dt>
<dd>

<p>Returning blessed objects from threads does not work. Depending on the classes involved, you may be able to work around this by returning a serialized version of the object (e.g., using <a href="./Data::Dumper.html">Data::Dumper</a> or <a href="Storable.html">Storable</a>), and then reconstituting it in the joining thread. If you&#39;re using Perl 5.10.0 or later, and if the class supports <a href="./threads::shared.html#OBJECTS">shared objects</a>, you can pass them via <a href="./Thread::Queue.html">shared queues</a>.</p>

</dd>
<dt id="END-blocks-in-threads"><a class="permalink" href="threads.html#END-blocks-in-threads">#</a><a id="END"></a>END blocks in threads</dt>
<dd>

<p>It is possible to add <a href="perlmod.html#BEGIN%2C-UNITCHECK%2C-CHECK%2C-INIT-and-END">END blocks</a> to threads by using <a href="perlfunc.html#require-VERSION">require</a> or <a href="perlfunc.html#eval-EXPR">eval</a> with the appropriate code. These <code>END</code> blocks will then be executed when the thread&#39;s interpreter is destroyed (i.e., either during a <code>-&gt;join()</code> call, or at program termination).</p>

<p>However, calling any <a href="threads.html">threads</a> methods in such an <code>END</code> block will most likely <i>fail</i> (e.g., the application may hang, or generate an error) due to mutexes that are needed to control functionality within the <a href="threads.html">threads</a> module.</p>

<p>For this reason, the use of <code>END</code> blocks in threads is <b>strongly</b> discouraged.</p>

</dd>
<dt id="Open-directory-handles"><a class="permalink" href="threads.html#Open-directory-handles">#</a><a id="Open"></a>Open directory handles</dt>
<dd>

<p>In perl 5.14 and higher, on systems other than Windows that do not support the <code>fchdir</code> C function, directory handles (see <a href="perlfunc.html#opendir-DIRHANDLE%2CEXPR">opendir</a>) will not be copied to new threads. You can use the <code>d_fchdir</code> variable in <a href="Config.html">Config.pm</a> to determine whether your system supports it.</p>

<p>In prior perl versions, spawning threads with open directory handles would crash the interpreter. <a href="https://rt.perl.org/rt3/Public/Bug/Display.html?id=75154">[perl #75154]</a></p>

</dd>
<dt id="Detached-threads-and-global-destruction"><a class="permalink" href="threads.html#Detached-threads-and-global-destruction">#</a><a id="Detached"></a>Detached threads and global destruction</dt>
<dd>

<p>If the main thread exits while there are detached threads which are still running, then Perl&#39;s global destruction phase is not executed because otherwise certain global structures that control the operation of threads and that are allocated in the main thread&#39;s memory may get destroyed before the detached thread is destroyed.</p>

<p>If you are using any code that requires the execution of the global destruction phase for clean up (e.g., removing temp files), then do not use detached threads, but rather join all threads before exiting the program.</p>

</dd>
<dt id="Perl-Bugs-and-the-CPAN-Version-of-threads"><a class="permalink" href="threads.html#Perl-Bugs-and-the-CPAN-Version-of-threads">#</a><a id="Perl1"></a>Perl Bugs and the CPAN Version of <a href="threads.html">threads</a></dt>
<dd>

<p>Support for threads extends beyond the code in this module (i.e., <i>threads.pm</i> and <i>threads.xs</i>), and into the Perl interpreter itself. Older versions of Perl contain bugs that may manifest themselves despite using the latest version of <a href="threads.html">threads</a> from CPAN. There is no workaround for this other than upgrading to the latest version of Perl.</p>

<p>Even with the latest version of Perl, it is known that certain constructs with threads may result in warning messages concerning leaked scalars or unreferenced scalars. However, such warnings are harmless, and may safely be ignored.</p>

<p>You can search for <a href="threads.html">threads</a> related bug reports at <a href="https://rt.cpan.org/Public/">https://rt.cpan.org/Public/</a>. If needed submit any new bugs, problems, patches, etc. to: <a href="https://rt.cpan.org/Public/Dist/Display.html?Name=threads">https://rt.cpan.org/Public/Dist/Display.html?Name=threads</a></p>

</dd>
</dl>

<h1 id="REQUIREMENTS"><a class="permalink" href="threads.html#REQUIREMENTS">#</a>REQUIREMENTS</h1>

<p>Perl 5.8.0 or later</p>

<h1 id="SEE-ALSO"><a class="permalink" href="threads.html#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1>

<p>threads on MetaCPAN: <a href="https://metacpan.org/release/threads">https://metacpan.org/release/threads</a></p>

<p>Code repository for CPAN distribution: <a href="https://github.com/Dual-Life/threads">https://github.com/Dual-Life/threads</a></p>

<p><a href="./threads::shared.html">threads::shared</a>, <a href="perlthrtut.html">perlthrtut</a></p>

<p><a href="https://www.perl.com/pub/a/2002/06/11/threads.html">https://www.perl.com/pub/a/2002/06/11/threads.html</a> and <a href="https://www.perl.com/pub/a/2002/09/04/threads.html">https://www.perl.com/pub/a/2002/09/04/threads.html</a></p>

<p>Perl threads mailing list: <a href="https://lists.perl.org/list/ithreads.html">https://lists.perl.org/list/ithreads.html</a></p>

<p>Stack size discussion: <a href="https://www.perlmonks.org/?node_id=532956">https://www.perlmonks.org/?node_id=532956</a></p>

<p>Sample code in the <i>examples</i> directory of this distribution on CPAN.</p>

<h1 id="AUTHOR"><a class="permalink" href="threads.html#AUTHOR">#</a>AUTHOR</h1>

<p>Artur Bergman &lt;sky AT crucially DOT net&gt;</p>

<p>CPAN version produced by Jerry D. Hedden &lt;jdhedden AT cpan DOT org&gt;</p>

<h1 id="LICENSE"><a class="permalink" href="threads.html#LICENSE">#</a>LICENSE</h1>

<p>threads is released under the same license as Perl.</p>

<h1 id="ACKNOWLEDGEMENTS"><a class="permalink" href="threads.html#ACKNOWLEDGEMENTS">#</a>ACKNOWLEDGEMENTS</h1>

<p>Richard Soderberg &lt;perl AT crystalflame DOT net&gt; - Helping me out tons, trying to find reasons for races and other weird bugs!</p>

<p>Simon Cozens &lt;simon AT brecon DOT co DOT uk&gt; - Being there to answer zillions of annoying questions</p>

<p>Rocco Caputo &lt;troc AT netrus DOT net&gt;</p>

<p>Vipul Ved Prakash &lt;mail AT vipul DOT net&gt; - Helping with debugging</p>

<p>Dean Arnold &lt;darnold AT presicient DOT com&gt; - Stack size API</p>


      </div>
      <div id="footer">
        <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p>

<p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p>


      </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
    <script src="../highlight.pack.js"></script>
    <script>hljs.highlightAll();</script>
  </body>
</html>
